﻿Imports RapidConsole

Module Module1
    Private program As New Program

    Sub Main()
        program.Start()
    End Sub
End Module

Public Class Program
    Inherits CommandPromptProgram

    Private WithEvents infoWindow As Window 'use a window as a message box
    Private WithEvents menuWindow As MenuWindow  'a scrollable list of items
    Private WithEvents listWindow As ListWindow
    Private dotX, dotY As Integer 'position of a user-drawn "dot" character on the screen

    Public Overrides Sub Start()
        UseF1Help = True
        infoWindow = New Window(Console.WindowWidth / 2 - 13, Console.WindowHeight / 2 - 4, 26, 8)
        infoWindow.BackColor = ConsoleColor.Red
        infoWindow.ForeColor = ConsoleColor.White
        infoWindow.FrameColor = ConsoleColor.Yellow

        Dim panel As New Window(4, 2, 30, 14) 'use a window as a panel
        panel.BackColor = ConsoleColor.DarkBlue
        panel.MergeFrameWithParent = False
        panel.CanFocus = False
        Output.Children.Add(panel)
        panel.Show()

        listWindow = New ListWindow(1, 1, 8, 8)
        panel.Children.Add(listWindow)
        listWindow.MergeFrameWithParent = False
        listWindow.BackColor = ConsoleColor.DarkGreen
        listWindow.FrameColor = ConsoleColor.Green
        listWindow.ForeColor = ConsoleColor.Yellow

        menuWindow = New MenuWindow(1, 1, 78)
        menuWindow.MergeFrameWithParent = False
        menuWindow.BackColor = ConsoleColor.DarkRed
        menuWindow.FrameColor = ConsoleColor.Red
        menuWindow.ForeColor = ConsoleColor.Yellow
        'menuWindow.FrameVisible = False

        For i As Integer = 1 To 20 'add items to the "menu" list window
            menuWindow.Add($" Item {i} ")
            listWindow.Add($"Menu Item {i}")
        Next

        MyBase.Start() 'start the program execution; this is a blocking method so call it last
    End Sub

    'implement the commands recognized by your program

    <CommandMethod()>
    Public Sub Test(line As CommandLine)
        Output.AppendText(line.GetParameterString, True)
    End Sub

    <CommandMethod()>
    Public Sub Testing(line As CommandLine)
        Output.AppendText(line.GetParameterString, True)
        line.CustomStatus = "OK - LONG FORM"
    End Sub

    <CommandMethod("", "Show {what} (args)", "Show dot (x y)\nShow info (infoText)")>
    Public Sub Show(line As CommandLine)
        If line.ParameterCount > 0 Then
            Select Case line.ParameterValue(0)
                Case "dot"
                    Dim x As Integer = 0
                    If line.ParameterCount > 1 Then
                        x = CInt(line.ParameterValue(1))
                    End If
                    Dim y As Integer = 0
                    If line.ParameterCount > 2 Then
                        y = CInt(line.ParameterValue(2))
                    End If
                    Window.UserBuffer.Clear(dotX, dotY)
                    Window.UserBuffer.Item(x, y) = "∙"c
                    Window.UserBuffer.ForegroundColor(x, y) = ConsoleColor.Red
                    dotX = x
                    dotY = y
                Case "info"
                    If line.ParameterCount > 1 Then
                        infoWindow.Content.Text = line.ParameterValue(1)
                        infoWindow.Show()
                    End If
                Case "menu"
                    menuWindow.Show()
                Case "list"
                    listWindow.Show()
                Case Else
                    line.CustomStatus = "UNKNOWN ARGUMENT"
            End Select

        Else
            line.CustomStatus = "INSUFFICIENT NUMBER OF ARGUMENTS"
        End If
    End Sub

    'handle the events on your program's windows required to implement your program's functionality

    Private Sub infoWindow_KeyProcessing(sender As Object, e As Window.KeyProcessingEventArgs) Handles infoWindow.KeyProcessing
        If e.Key.Key = ConsoleKey.Enter Then
            CType(sender, Window).Hide()
        End If
    End Sub

    Private Sub menuWindow_ItemSelected(sender As Object, e As EventArgs) Handles menuWindow.ItemSelected
        Output.AppendText($"Selected: {menuWindow.Item(menuWindow.SelectedIndex)}", True)
    End Sub
End Class